{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Running Sum of 1d Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #prefix-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #前缀和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: runningSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #一维数组的动态和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个数组 <code>nums</code> 。数组「动态和」的计算公式为：<code>runningSum[i] = sum(nums[0]&hellip;nums[i])</code> 。</p>\n",
    "\n",
    "<p>请返回 <code>nums</code> 的动态和。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>[1,3,6,10]\n",
    "<strong>解释：</strong>动态和计算过程为 [1, 1+2, 1+2+3, 1+2+3+4] 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,1,1,1,1]\n",
    "<strong>输出：</strong>[1,2,3,4,5]\n",
    "<strong>解释：</strong>动态和计算过程为 [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1] 。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [3,1,2,10,1]\n",
    "<strong>输出：</strong>[3,4,6,16,17]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>-10^6&nbsp;&lt;= nums[i] &lt;=&nbsp;10^6</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [running-sum-of-1d-array](https://leetcode.cn/problems/running-sum-of-1d-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [running-sum-of-1d-array](https://leetcode.cn/problems/running-sum-of-1d-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4]', '[1,1,1,1,1]', '[3,1,2,10,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\r\n",
    "        for i in range(1, len(nums)):\r\n",
    "            nums[i] = nums[i-1] + nums[i]\r\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        nums_len = len(nums)\n",
    "        if nums_len == 0:\n",
    "            return nums\n",
    "        \n",
    "        for i in range(1, nums_len):\n",
    "            nums[i] += nums[i-1]\n",
    "        return nums\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(1, len(nums)):\n",
    "            nums[i] += nums[i-1]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        a = 0\n",
    "        sum_list = []\n",
    "        for i in nums:\n",
    "            a += i\n",
    "            sum_list.append(a)\n",
    "        return sum_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        l=[0 for i in nums]\n",
    "        for i in range(len(nums)):\n",
    "            l[i]=sum(nums[0:i+1])\n",
    "        return(l)\n",
    "    \n",
    "        #return([sum(nums[0:i+1]) for i in range(len(nums))])\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        # for i in range(1, len(nums)):\n",
    "        #     nums[i] += nums[i-1]\n",
    "        # return nums\n",
    "        self.nums = nums\n",
    "        self.foo(0, len(self.nums) - 1)\n",
    "        return self.nums\n",
    "        \n",
    "    def foo(self, a: int, b: int) -> int:\n",
    "        if a < b:\n",
    "            self.nums[b] += self.foo(a, b - 1)\n",
    "        return self.nums[b]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        def d(a):\n",
    "            if a == 0:\n",
    "                return nums[0]\n",
    "            else:\n",
    "                return nums[a] + d(a-1)\n",
    "        for i in range(len(nums)):\n",
    "            if i == 0:\n",
    "                result.append(nums[0])\n",
    "            else:\n",
    "                result.append(d(i))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        res = [0]*n\n",
    "        def test(nums, n):\n",
    "            if n == 0:\n",
    "                return nums[0]\n",
    "            else:\n",
    "                return test(nums, n-1) + nums[n]\n",
    "        for i in range(n):\n",
    "            res[i] = test(nums, i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "      l = len(nums)\n",
    "      for i in range(1,l):\n",
    "          nums[i] += nums[i-1]\n",
    "      return nums "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        sums=nums\n",
    "        \n",
    "        for i in range(1,len(nums)):\n",
    "            sums[i]+=nums[i-1]\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        return accumulate (nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        sum_list = []\n",
    "        for i, cur_num in enumerate(nums):\n",
    "            if i == 0:\n",
    "                sum_list.append(cur_num)\n",
    "                continue\n",
    "            new_sum = sum_list[-1] + cur_num\n",
    "            sum_list.append(new_sum)\n",
    "        return sum_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        sum=[0 for x in range(len(nums))]\n",
    "        for i,value in enumerate(nums):\n",
    "            if i>0:\n",
    "                sum[i]=nums[i]+sum[i-1]\n",
    "            else:\n",
    "                sum[i]=nums[i]\n",
    "        print(sum)\n",
    "        return sum    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        refuse = []  # 输出\n",
    "        print(range(len(nums)))\n",
    "        for i in range(len(nums)):\n",
    "            print(nums[:i+1])\n",
    "            refuse.append(sum(nums[:i+1]))\n",
    "        return refuse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        for j in range(1, len(nums)):\n",
    "            nums[j] = nums[j] + nums[j - 1]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(1,len(nums)):\n",
    "            nums[i]+=nums[i-1]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runningSum(self, nums: List[int]) -> List[int]:\n",
    "        sum = [nums[0]]\n",
    "        for i in range(1,len(nums)):\n",
    "            sum.append(nums[i]+sum[i-1])\n",
    "        return sum"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
